package end.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import end.bean.shiro.Resource;

/**
 * 
 * ResourceTree 上午10:00:07 
 * Copyright Zhaocj Inc. All right reserved. 
 * Email : alldios@outlook.com
 * 为避免id=parentid的情况出现
 * 
 */
public class ResourceTreeTwo implements Serializable {
	private static final long serialVersionUID = 6145961932017984738L;
	public static Map<String, Object> mapArray = new LinkedHashMap<String, Object>();
	public List<Resource> menuCommon;
	public String typeO;
	public String typeT;
	public List<Object> list = new ArrayList<Object>();

	public ResourceTreeTwo(String typeO, String typeT) {
		super();
		this.typeO = typeO;
		this.typeT = typeT;
	}
	public List<Object> menuList(List<Resource> resource) {
		this.menuCommon = resource;
		for (Resource x : resource) {
			Map<String, Object> mapArr = new LinkedHashMap<String, Object>();
			if (x.getParentId() == 0) {
				mapArr.put("id", x.getId());
				mapArr.put("text", x.getName());
				mapArr.put("type", x.getType());
				mapArr.put("checked", false);
				if (x.getType().equals(typeO)) {
					if (!menuChild(x.getId(),x.getType()).isEmpty()) {
						mapArr.put("iconCls", "icon-parent-1");
						mapArr.put("state", "open");
					} else {
						mapArr.put("iconCls", "icon-child-1");
					}
				}
				if (x.getType().equals(typeO)) {
					if (!menuChild(x.getId(),x.getType()).isEmpty()) {
						mapArr.put("children", menuChild(x.getId(),x.getType()));
					} else {
						mapArr.put("children", null);
					}
				}
				list.add(mapArr);
			}
		}
		return list;
	}
	public List<?> menuChild(int id,String type) {
		List<Object> lists = new ArrayList<Object>();
		for (Resource a : menuCommon) {
			Map<String, Object> childArray = new LinkedHashMap<String, Object>();
			if (a.getParentId() == id&&!a.getType().equals(type)) {
				childArray.put("id", a.getId());
				childArray.put("text", a.getName());
				childArray.put("type", a.getType());
				childArray.put("checked", false);
				if (a.getType().equals(typeO)) {
					if (!menuChild(a.getId(),a.getType()).isEmpty()) {
						childArray.put("iconCls", "icon-parent-1");
						childArray.put("state", "closed");
					} else {
						childArray.put("iconCls", "icon-child-1");
					}
				}
				if (a.getType().equals(typeO)) {
					if (!menuChild(a.getId(),a.getType()).isEmpty()) {
						childArray.put("children", menuChild(a.getId(),a.getType()));
					} else {
						childArray.put("children", null);
					}
				}
				if (a.getType().equals(typeT)) {
					if (!menuChild(a.getId(),a.getType()).isEmpty()) {
						childArray.put("iconCls", "icon-parent-1");
					} else {
						childArray.put("iconCls", "icon-child-1");
					}
					childArray.put("children", menuChild(a.getId(),a.getType()));
				}
				lists.add(childArray);
			}
		}
		return lists;
	}
}
